//
// Copyright (c) MiTAC Corp. All rights reserved.
//------------------------------------------------------------------------------
//
//  File:  key.c
//  Programmer: Visual.Wei
//  Date: 2007-4-24
//
#include <windows.h>
#include <eboot.h>
#include <ipl_core.h>

void InitKeypad()
{
	S3C2443_IOPORT_REG *pIOPortReg;

	pIOPortReg = (S3C2443_IOPORT_REG*)OALPAtoVA(S3C2443_BASE_REG_PA_IOPORT, FALSE);	

	//GPG0 EINT8
	//GPG1 EINT9
	//GPG4 EINT12
	//GPG6 EINT14
	CLRREG32(&pIOPortReg->GPGCON, (3 << 2*0)|(3 << 2*1)|(3 << 2*4)|(3 << 2*6));
	SETREG32(&pIOPortReg->GPGCON, (2 << 2*0)|(2 << 2*1)|(2 << 2*4)|(2 << 2*6));
	//disable pull up/down
	SETREG32(&pIOPortReg->GPGUDP, (3 << 2*0)|(3 << 2*1)|(3 << 2*4)|(3 << 2*6));
	SETREG32(&pIOPortReg->EXTINT1, (0xF << 4*0)|(0xF << 4*1)|(0xF << 4*4)|(0xF << 4*6));
	//SETREG32(&pIOPortReg->EXTINT1, (8 << 4*0)|(8 << 4*1)|(8 << 4*4)|(8 << 4*6));
}
	
BOOL IsChordKeyPressed(DWORD val, DWORD timeout)
{
	int key = KEY_NONE;
	S3C2443_IOPORT_REG *pIOPortReg;
	DWORD time=timeout*10000;
	
#define IS_LOW(id) (!(INREG32(&pIOPortReg->GPGDAT)&(0x1<<id)))

	pIOPortReg = (S3C2443_IOPORT_REG*)OALPAtoVA(S3C2443_BASE_REG_PA_IOPORT, FALSE);	

	time=IPLGetTickCount();
	
	do
	{
		if (IS_LOW(6))
		{
			RETAILMSG(0,(TEXT("up\r\n")));
			key = KEY_TUP;
		}
		else if (IS_LOW(4) )
			key = KEY_TDOWN;
		else if (IS_LOW(0))
			key = KEY_TACTION;
		else 
			key = KEY_NONE;
		if(key==val)
			if(IPLGetTickCount()-time>timeout*10)
				return TRUE;
	}while(key==val);
	
	return FALSE;
}